<template>
	<view class="">
		<view class="cu-modal" :class="DialogModal ?'show':''" @click="DialogModal=''" v-if="DialogModal">
			<view class="cu-dialog bg-white login_pop" @click.stop>
				<view class="lp_Close" @click="DialogModal=''">
					<text class="cuIcon-close text-red"></text>
				</view>
				<image src="../../static/image/loginPop.png" mode="" class="l_img"></image>
				<view class="text-black text-bold margin-top font-30">您还未登录</view>
				<view class="margin-top-xs font-28 text-gray">请先登录再进行此操作</view>
				<button class="cu-btn bg-green margin-top" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber"
					:disabled="loading" :loading="loading">一键登录</button>
			</view>
		</view>
	</view>
</template>

<script>
	import api from 'utils/api'

	export default {
		name: 'loginPop',
		data() {
			return {
				DialogModal: false,
				loading: false,
				pageUrl: null, // 登录后跳转页面的路径
			}
		},
		methods: {
			open(url) {
				this.pageUrl = url
				this.DialogModal = true
			},
			getPhoneNumber(e) {
				console.log(e, 1111111111)
				if (e.detail.encryptedData) {
					this.loading = true
					// uni.login({
					// 	success: (res) => {
					// 		if (res.code) {
					// 			api.loginWxMa2({
					// 				jsCode: res.code
					// 			}).then(res => {
					// 				uni.setStorageSync('third_session', res.data.thirdSession);
					// 				uni.setStorageSync('user_info', res.data);
					this.getPhone(e)
					// 			});
					// 		}
					// 	}
					// });
				}
			},
			getPhone(e) {
				api.loginWxMa({
					encryptedData: e.detail.code,
					iv: e.detail.iv,
					headimgUrl: 'https://future-app.oss-cn-hangzhou.aliyuncs.com/ui-image/my_default.png'
				}).then(res => {
					let userInfo = res.data;
					uni.setStorageSync('third_session', userInfo.thirdSession);
					uni.setStorageSync('user_info', userInfo);
					uni.showToast({
						title: '登录成功',
						icon: 'success',
						duration: 1000
					});
					setTimeout(() => {
						if (userInfo && userInfo.userType == 3) {
							uni.navigateTo({
								url: '/pages/actYc/act-yc?activityId=43'
							})
						} else {
							uni.reLaunch({
								url: '/pages/home/index'
							});
						}
					}, 1000)

				}).finally(() => {
					this.loading = false
				})
			}

		}
	}
</script>

<style scoped>
	.login_pop {
		padding: 40rpx 80rpx;
		width: auto;
		position: relative;
	}

	.login_pop .lp_Close {
		position: absolute;
		top: 0;
		right: 0;
		padding: 25rpx 25rpx 40rpx 40rpx;
	}

	.login_pop .l_img {
		width: 260rpx;
		height: 260rpx;
		border-radius: 50%;
	}
</style>